import { defineStore } from 'pinia'
import { TemplateApi } from '@/api/templates'
import { type ListData } from 'axios'

export const testData = [
  {
    id: 1,
    coverImg: 'https://static.imooc-lego.com/upload-files/screenshot-889755.png',
    title: 'test title 1',
    author: 'viking',
    copiedCount: 1
  },
  {
    id: 2,
    coverImg: 'https://static.imooc-lego.com/upload-files/screenshot-677311.png',
    title: '前端架构师直播海报',
    author: 'viking',
    copiedCount: 1
  },
  {
    id: 3,
    coverImg: 'https://static.imooc-lego.com/upload-files/screenshot-682056.png',
    title: '前端架构师直播海报',
    author: 'viking',
    copiedCount: 1
  },
  {
    id: 4,
    coverImg: 'https://static.imooc-lego.com/upload-files/screenshot-677311.png',
    title: '前端架构师直播海报',
    author: 'viking',
    copiedCount: 1
  },
  {
    id: 5,
    coverImg: 'https://static.imooc-lego.com/upload-files/screenshot-889755.png',
    title: '前端架构师直播海报',
    author: 'viking',
    copiedCount: 1
  },
  {
    id: 6,
    coverImg: 'https://static.imooc-lego.com/upload-files/screenshot-677311.png',
    title: '前端架构师直播海报',
    author: 'viking',
    copiedCount: 1
  }
]

/**
 * 模板列表
 * @methods setTemplatesList 设置模板数据
 */
export const templateInfos = defineStore('templatesList', {
  state: (): TemplatesProps => ({
    templateList: [],
    totalTemplates: 0,
    workList: [],
    totalWorks: 0
  }),
  actions: {
    async setTemplatesList() {
      const [e, r] = await TemplateApi.getTemplates<ListData<TemplateProps>>({
        opName: 'getTemplates'
      })
      if (r && r.errno === 0) {
        this.templateList = r.data.list
        this.totalWorks = r.data.count
      }
    },
    async setWorkList() {
      const [e, r] = await TemplateApi.getTemplates<ListData<WorkProp>>({
        opName: 'getTemplates'
      })
      if (r && r.errno === 0) {
        this.workList = r.data.list
      }
    }
  },
  getters: {
    getTemplateById: (state) => {
      return (id: number) => state.templateList.find((t) => t.id === id)
    }
  }
})
